<?php 
$req_path="../";
include($req_path."includes/ini.php");
include($req_path."includes/db.php");

class M_Car extends DB{
    private $rowCount=0;
    public $table_name="tb_car";
	
    function M_Car(){
		parent::__construct();
    }
   
    function loadData(){
      $sqlscript= "   SELECT id, license, detail ,amount ,status";
	  $sqlscript.= " FROM tb_car cr ";
        
        $result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
    }
	function getDropdownCar(){
		$sql="SELECT id, license , amount 
				FROM tb_car WHERE status='Y' Order by id asc" ;
				
		$result = $this->ExecuteData($sql);
		while($row = $result->FetchRow()){
			$ar[$row["id"]] = $row["amount"] .' ที่นั่ง  (' .$row['license'].')';
		}
		return $ar;	
	}
	function load_car_data($id){
		$sql = "SELECT c.id, c.detail, cm.cost , cm.detail as cost_detail, cm.`status`
				FROM tb_car_manage cm
				JOIN tb_car c on c.id = cm.car_id";
		 if ($id!=0){
			$sql .=" WHERE cm.id=".$id;
		 }
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	function loadRoute($id){
		$sql = " SELECT route,route_time, DATE_FORMAT( start_date ,'%d/%m/%Y') startdate
				,DATE_FORMAT(start_date,'%m') start_month
				,DATE_FORMAT(start_date,'%d') start_day
				,DATE_FORMAT(start_date,'%Y') start_year
				FROM tb_car_route WHERE car_manage_id=".$id;
		$result = $this->ExecuteData($sql);
		$this->rowCount = $result->RecordCount();
		return $result;
	}
	function loadCarMange($year='',$term='',$status='0',$id=0){
		$sql = "SELECT cm.id, c.detail, cm.cost , cm.detail as cost_detail, cm.`status`
				,cm.remark, cm.driver,cm.telephone, cm.cost_type, tp.name as type_name, cm.year, cm.term, cm.car_id
				FROM tb_car_manage cm
				JOIN tb_type_payment_service tp on tp.id = cm.cost_type
				JOIN tb_car c on c.id = cm.car_id WHERE 1=1";
		if ($year!=''){
			$sql.=" AND cm.year=".$year;
		}
		if ($term!=''){
			$sql.=" AND cm.term=".$term;
		}
		if ($status!='0'){
			$sql.=" AND cm.status='".$status."'";
		}
		if ($id !=0){
			$sql.=" AND cm.id=".$id;
		}
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	function insertDataDetail($val){
		if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT year, term ,car_id ,driver , telephone, cost,cost_type, detail , remark, status ,createdate,createby FROM tb_car_manage where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
				$id = $this->getLastID();
				return $id;
            }
	}
	function updateDataDetail($id,$val){
		if (isset($val)){
				$sql=$this->UpdateSqlScript("SELECT year, term ,car_id ,driver , telephone, cost,cost_type, detail , remark, status ,createdate,createby FROM tb_car_manage where id=".$id, $val);
				$this->ExecuteData($sql);
            }
	}
	function insertRoute($val){
		if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT car_manage_id, route , route_time , start_date  FROM tb_car_route where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
            }
	}
	
	function getDropdownCarDetail($id){
		$sql =" SELECT detail ";
		$sql.=" FROM tb_car WHERE id=".$id;
		
		$result = $this->ExecuteData($sql);
		$row = $result->FetchRow();
		
		return $row;	
	}
    function getRowCount(){
       return $this->rowCount;
    }	
 
    function insertData($val){
            if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT license, detail ,amount ,status ,createdate,createby FROM tb_car where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
            }
    }
    function updateData($id,$val){
            if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript("SELECT license, detail ,amount ,status , updatedate, updateby FROM tb_car where id='".$id."'" ,$val);
				 $this->ExecuteData($sqlUpdate);
				// return $sqlUpdate;
            }
    }
	function deleDetail($id){
			$sqlDelete = "Delete FROM tb_car_route where car_manage_id='".$id."'";
			$this->ExecuteData($sqlDelete);
	}
	
	function deleManage($id){
	
			$sqlDelete = "Delete FROM tb_car_manage where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			
			$this->deleDetail($id);
	}
    function deleData($id){
            if(isset($id)){
			$sqlDelete = "Delete FROM tb_car where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			 
			return 1;
            }
    }
    
}
?>
